package com.mk.property.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mk.property.common.R;
import com.mk.property.pojo.Department;
import com.mk.property.pojo.Employee;
import com.mk.property.pojo.Owner;
import com.mk.property.service.DepartmentService;
import com.mk.property.service.EmployeeService;
import com.mk.property.vo.DepartmentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @Autowired
    EmployeeService employeeService;

    /**
     * 分页查询
     *
     * @param page     当前查询页码
     * @param pageSize 每页展示的记录数
     * @param name     部门名称 - 可选参数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //TODO 这里以后可以加个排序,数据库里面加个updateTime字段

        //构造分页条件对象
        Page<Department> pageInfo = new Page(page, pageSize);
        Page<DepartmentVO> departmentVOPage = new Page<>();

        //构建查询及排序条件
        //条件构造器
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();
        //name不为空才会执行下面的语句
        lqw.like(StringUtils.isNotEmpty(name), Department::getName, name);

        //执行分页条件查询
        departmentService.page(pageInfo, lqw);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, departmentVOPage, "records");
        List<Department> records = pageInfo.getRecords();

        //遍历分页查询列表数据,根据leaderId查询Employee表,从而获取经理名称
        List<DepartmentVO> list = records.stream().map((item) -> {
            DepartmentVO departmentVO = new DepartmentVO();
            BeanUtils.copyProperties(item, departmentVO);
            Long leaderId = item.getLeaderId();
            Employee leader = employeeService.getById(leaderId);
            if (leader != null) {
                String leaderName = leader.getName();
                departmentVO.setLeaderName(leaderName);
            }
            return departmentVO;
        }).collect(Collectors.toList());

        //封装数据并返回
        departmentVOPage.setRecords(list);
        return R.success(departmentVOPage);
    }

    /**
     * 新增部门
     *
     * @param department
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Department department) {
        log.info("新增部门,部门信息为{}", department.toString());
        departmentService.save(department);
        return R.success("新增部门成功");
    }

    /**
     * 根据id查询部门信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Department> getById(@PathVariable Long id) {
        log.info("根据id查询部门信息");
        Department department = departmentService.getById(id);
        if (department != null) {
            return R.success(department);
        }
        return R.error("没有查询到对应部门信息");
    }

    @GetMapping("/id")
    public R<List> getDepartmentIdAndNameList() {
        //查询全部业主数据
        List<Department> departments = departmentService.list();
        return R.success(departments);
    }

    /**
     * 修改部门信息
     *
     * @param department
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Department department) {
        log.info(department.toString());
        departmentService.updateById(department);
        return R.success("部门信息修改成功");
    }
}
